<?php defined('BASEPATH') OR exit('No direct script access allowed');

class Users_class extends CI_Model {

	public function check_login($post){
		$strEmail = stripslashes(filter_var($post['strEmail'], FILTER_SANITIZE_STRING));
		$strPassword = stripslashes(filter_var($post['strPassword'], FILTER_SANITIZE_STRING));
		$strCaptcha = stripslashes(filter_var($post['strCaptcha'], FILTER_SANITIZE_STRING));
		$strReCaptcha = stripslashes(filter_var($post['reCaptchaHidden'], FILTER_SANITIZE_STRING));
		
		if($strEmail=='')
		{
			$data['intStatus'] = 1; // Email is empty!
		}
		else if($strPassword=='')
		{
			$data['intStatus'] = 2; // Password is empty!
		}
		else if($strCaptcha=='')
		{
			$data['intStatus'] = 3; // Captcha is empty!
		}
		else
		{
			$getUserExists = $this->get_user_exist($post['strEmail']);
			if($getUserExists==TRUE){
				$query = $this->db->get_where('users',array('strEmail'=>$strEmail))->result_array();
				$query = current($query);	
				
				if($strCaptcha == $strReCaptcha){
					if(trim($query['intDisabled'])==1) {
						$data['intStatus'] = 4; // Access Denied!
					} else {
						if($this->encrypt->decode($query['strPassword']) == $strPassword) {
						//if($query['strPassword'] == $strPassword) {
							$data['intUserID'] = $query['intUserID'];
							$this->session->set_userdata('user_logged_in', 1);
							$this->session->set_userdata('intUserID', $query['intUserID']);
							$this->session->set_userdata('strEmail', $query['strEmail']);
							$this->session->set_userdata('intAccountType', $query['intAccountType']);
							$data['intStatus'] = 7;	// Login Success!				
						} else {
							$data['intStatus'] = 5; // Password Error!
						}
					}
				} else {
					$data['intStatus'] = 6; // Captcha Mis-matched!
				}	
			} else {
				$data['intStatus'] = 0; // E-mail doesn't exist!
			}
		}
		return $data;
	}
	public function update($post,$id)
	{
		$intUserID = stripslashes(filter_var($id, FILTER_SANITIZE_STRING));
		$strEmail =  stripslashes(filter_var($post['strEmail'], FILTER_SANITIZE_STRING));
		$strFirstName = stripslashes(filter_var($post['strFirstName'], FILTER_SANITIZE_STRING));
		if($post['strMiddleName']!='')
		{
			$strMiddleName = stripslashes(filter_var($post['strMiddleName'], FILTER_SANITIZE_STRING));
		}
		else
		{
			$strMiddleName = '';
		}
		$strLastName = stripslashes(filter_var($post['strLastName'], FILTER_SANITIZE_STRING));
		$strPassword = stripslashes(filter_var($this->encrypt->encode($post['strPassword']), FILTER_SANITIZE_STRING));
		if($this->session->userdata('intAccountType')=='1')
		{
			$strAccountType = stripslashes(filter_var($post['strAccountType'], FILTER_SANITIZE_STRING));
		}
		else
		{
			$strAccountType = $this->session->userdata('intAccountType');
		}
			
		$data = array(
				'strEmail' => $strEmail,
				'strFirstName' => $strFirstName,
				'strMiddleName' => $strMiddleName,
				'strLastName' => $strLastName,
				'strPassword' => $strPassword,
				'intAccountType' => $strAccountType,
				'intDisabled' => 0,
				'dateModified'=>date('Y-m-d H:i:s')
		);
		$getUserExists = $this->get_user_exist($post['strEmail']);
		if($getUserExists==FALSE || $strEmail==$this->session->userdata('strEmail') || $this->session->userdata('intAccountType')=='1'){
			$this->db->where(array('intUserID'=>$intUserID));
			$this->db->update('users', $data);
		
			if($this->db->_error_number()==0)
			{
				$data['intStatus'] = 1; // Success
				$this->update_info($post,$id);
			}
			else
			{
				$data['intStatus'] = 0; // Failed
			}
		}
		else
		{
			$data['intStatus'] = 2; // Email Exist
		}
		return $data;
	}
	public function update_info($post,$id)
	{
		$intUserID = stripslashes(filter_var($id, FILTER_SANITIZE_STRING));
		$strGender = stripslashes(filter_var($post['strGender'], FILTER_SANITIZE_STRING));
		$strCivilStatus = stripslashes(filter_var($post['strCivilStatus'], FILTER_SANITIZE_STRING));
		$strMonth = stripslashes(filter_var($post['strMonth'], FILTER_SANITIZE_STRING));
		$strDays = stripslashes(filter_var($post['strDays'], FILTER_SANITIZE_STRING));
		$strYear = stripslashes(filter_var($post['strYear'], FILTER_SANITIZE_STRING));
		$country = stripslashes(filter_var($post['country'], FILTER_SANITIZE_STRING));
		$strAddress = stripslashes(filter_var($post['strAddress'], FILTER_SANITIZE_STRING));
		$strCity = stripslashes(filter_var($post['strCity'], FILTER_SANITIZE_STRING));
		$strHometown = stripslashes(filter_var($post['strHometown'], FILTER_SANITIZE_STRING));
		$strProvince = stripslashes(filter_var($post['strProvince'], FILTER_SANITIZE_STRING));
		$strMobile = stripslashes(filter_var($post['strMobile'], FILTER_SANITIZE_STRING));
		$strLandline = stripslashes(filter_var($post['strLandline'], FILTER_SANITIZE_STRING));

		$data = array(
				'strGender' => $strGender,
				'strCivilStatus' => $strCivilStatus,
				'birthMonth' => $strMonth,
				'birthDay' => $strDays,
				'birthYear' => $strYear,
				'strAddress' => $strAddress,
				'strCity' => $strCity,
				'strHometown' => $strHometown,
				'strProvince'=> $strProvince,
				'strCountry'=> $country,
				'strMobile'=> $strMobile,
				'strLandline'=> $strLandline
		);
		
		$this->db->where(array('strUserInfoID'=>$intUserID));
		$this->db->update('user_info', $data);
	}
	public function get_user_exist($post){
		$strEmail = stripslashes(filter_var($post, FILTER_SANITIZE_STRING));
		
		$this->db->where('strEmail',$strEmail);
		$query = $this->db->get('users');
		if( $query->num_rows() > 0 ){ return TRUE; } else { return FALSE; }
	}
	public function get_user_info($strEmail)
	{
		$strEmail = stripslashes(filter_var($strEmail, FILTER_SANITIZE_STRING));
		
		$this->db->where(array('strEmail'=>$strEmail,'intDisabled'=>0));
		$query = $this->db->get('users')->result_array();
		$query = current($query);
		
		if($query['intUserID']==""){
			return 0;
		} else {
			return $query;
		}
	}
	public function get_user_data($data)
	{
		$intUserID = stripslashes(filter_var($data['intUserID'], FILTER_SANITIZE_STRING));
		
		$this->db->select('*');
		$query = $this->db->from('users');
		$query = $this->db->join('user_info', 'users.intUserID = user_info.strUserInfoID', 'left');
		$query = $this->db->where(array('users.intUserID'=>$intUserID,'users.intDisabled'=>0));
		$query = $this->db->get()->result_array();
		$query = current($query);
			
		if($query['intUserID']==""){
			return 0;
		}else{
			return $query;
		}
	}
	public function user_lists($offset = null, $limit = null)
	{
		
		$this->db->select('*');
		$query = $this->db->from('users');
		$query = $this->db->join('user_info', 'users.intUserID = user_info.strUserInfoID', 'left');
		$query = $this->db->where(array('users.intDisabled'=>0));
			$query = $this->db->get();
		
		if ($query->num_rows() > 0){
			$result = $query->result_array();
			return $result;
		}else{
			return 0;
		}
	}
	public function get_user_data_total()
	{
		
		$this->db->select('*');
		$query = $this->db->from('users');
		$query = $this->db->join('user_info', 'users.intUserID = user_info.strUserInfoID', 'left');
		$query = $this->db->where(array('users.intDisabled'=>0));
		$result = $this->db->count_all_results();

		return $result;

	}
		public function reserved_lists($offset = null, $limit = null)
	{
		
		$this->db->select('*');
		$query = $this->db->from('reservation');
			$query = $this->db->get();
		
		if ($query->num_rows() > 0){
			$result = $query->result_array();
			return $result;
		}else{
			return 0;
		}
	}
	public function reserved_lists_total()
	{
		
		$this->db->select('*');
		$query = $this->db->from('reservation');
		$result = $this->db->count_all_results();

		return $result;

	}
	public function get_recipient($post)
	{
		$keywords = stripslashes(filter_var($post['keywords'], FILTER_SANITIZE_STRING));

		$this->db->select('intUserID, strFirstName, strMiddleName, strLastName, strEmail, intAccountType');
		$query = $this->db->from('users');
		$query = $this->db->where(array('intDisabled'=>0));
		$query = $this->db->like('strFirstName', $keywords);
		$query = $this->db->or_like(array('strMiddleName'=>$keywords,'strLastName'=>$keywords,'strEmail'=>$keywords));
		$query = $this->db->get();
		
		if ($query->num_rows() > 0){
			$result = $query->result_array();
			return $result;
		}else{
			return 0;
		}
	}
	public function save_resto_owner($post)
	{
		$strEmail =  stripslashes(filter_var($post['strEmail'], FILTER_SANITIZE_STRING));
		$strFirstName = stripslashes(filter_var($post['strFirstName'], FILTER_SANITIZE_STRING));
		if($post['strMiddleName']!='')
		{
			$strMiddleName = stripslashes(filter_var($post['strMiddleName'], FILTER_SANITIZE_STRING));
		}
		else
		{
			$strMiddleName = '';
		}
		$strLastName = stripslashes(filter_var($post['strLastName'], FILTER_SANITIZE_STRING));
		$strPassword = stripslashes(filter_var($this->encrypt->encode($post['strPassword']), FILTER_SANITIZE_STRING));
			
		$data = array(
				'strEmail' => $strEmail,
				'strFirstName' => $strFirstName,
				'strMiddleName' => $strMiddleName,
				'strLastName' => $strLastName,
				'strPassword' => $strPassword,
				'intAccountType' => 2,
				'intDisabled' => 0,
				'dateCreated'=>date('Y-m-d H:i:s'),
				'dateModified'=>date('Y-m-d H:i:s')
		);
		$getUserExists = $this->get_user_exist($post['strEmail']);
		if($getUserExists==FALSE){
			$this->db->insert('users', $data);
		
			if($this->db->_error_number()==0)
			{
				$data['intStatus'] = 1; // Success
				$intUserID = $this->db->insert_id();
				$this->session->set_userdata('intRestoOwer', $intUserID);
				$this->session->set_userdata('intRestoOwerName', $strFirstName.' '.$strMiddleName.' '.$strLastName);
				$this->save_owner_info($post,$intUserID);
			}
			else
			{
				$data['intStatus'] = 0; // Failed
			}
		}
		else
		{
			$data['intStatus'] = 2; // Email Exist
		}
		return $data;
	}
	public function save_branch_owner($post)
	{
		$strEmail =  stripslashes(filter_var($post['strEmail'], FILTER_SANITIZE_STRING));
		$strFirstName = stripslashes(filter_var($post['strFirstName'], FILTER_SANITIZE_STRING));
		if($post['strMiddleName']!='')
		{
			$strMiddleName = stripslashes(filter_var($post['strMiddleName'], FILTER_SANITIZE_STRING));
		}
		else
		{
			$strMiddleName = '';
		}
		$strLastName = stripslashes(filter_var($post['strLastName'], FILTER_SANITIZE_STRING));
		$strPassword = stripslashes(filter_var($this->encrypt->encode($post['strPassword']), FILTER_SANITIZE_STRING));
			
		$data = array(
				'strEmail' => $strEmail,
				'strFirstName' => $strFirstName,
				'strMiddleName' => $strMiddleName,
				'strLastName' => $strLastName,
				'strPassword' => $strPassword,
				'intAccountType' => 2,
				'intDisabled' => 0,
				'dateCreated'=>date('Y-m-d H:i:s'),
				'dateModified'=>date('Y-m-d H:i:s')
		);
		$getUserExists = $this->get_user_exist($post['strEmail']);
		if($getUserExists==FALSE){
			$this->db->insert('users', $data);
		
			if($this->db->_error_number()==0)
			{
				$data['intStatus'] = 1; // Success
				$intUserID = $this->db->insert_id();
				$this->session->set_userdata('intRestoOwer', $intUserID);
				$this->session->set_userdata('intRestoOwerName', $strFirstName.' '.$strMiddleName.' '.$strLastName);
				$this->save_owner_info($post,$intUserID);
			}
			else
			{
				$data['intStatus'] = 0; // Failed
			}
		}
		else
		{
			$data['intStatus'] = 2; // Email Exist
		}
		return $data;
	}
	public function save_owner_info($post,$intUserID)
	{
		$intUserID = stripslashes(filter_var($intUserID, FILTER_SANITIZE_STRING));
		$strGender = stripslashes(filter_var($post['strGender'], FILTER_SANITIZE_STRING));
		$strCivilStatus = stripslashes(filter_var($post['strCivilStatus'], FILTER_SANITIZE_STRING));
		$strMonth = stripslashes(filter_var($post['strMonth'], FILTER_SANITIZE_STRING));
		$strDays = stripslashes(filter_var($post['strDays'], FILTER_SANITIZE_STRING));
		$strYear = stripslashes(filter_var($post['strYear'], FILTER_SANITIZE_STRING));
		$country = stripslashes(filter_var($post['country'], FILTER_SANITIZE_STRING));
		$strAddress = stripslashes(filter_var($post['strAddress'], FILTER_SANITIZE_STRING));
		$strCity = stripslashes(filter_var($post['strCity'], FILTER_SANITIZE_STRING));
		$strHometown = stripslashes(filter_var($post['strHometown'], FILTER_SANITIZE_STRING));
		$strProvince = stripslashes(filter_var($post['strProvince'], FILTER_SANITIZE_STRING));
		$strMobile = stripslashes(filter_var($post['strMobile'], FILTER_SANITIZE_STRING));
		$strLandline = stripslashes(filter_var($post['strLandline'], FILTER_SANITIZE_STRING));

		$data = array(
				'strUserInfoID' => $intUserID,
				'strGender' => $strGender,
				'strCivilStatus' => $strCivilStatus,
				'birthMonth' => $strMonth,
				'birthDay' => $strDays,
				'birthYear' => $strYear,
				'strAddress' => $strAddress,
				'strCity' => $strCity,
				'strHometown' => $strHometown,
				'strProvince'=> $strProvince,
				'strCountry'=> $country,
				'strMobile'=> $strMobile,
				'strLandline'=> $strLandline
		);
		
		$this->db->insert('summ_user_info', $data);
	}
	public function resto_owner_dropdown()
	{
		$this->db->select('intUserID, strFirstName, strMiddleName, strLastName, strEmail, intAccountType');
		$query = $this->db->from('users');
		$query = $this->db->where(array('intAccountType'=>2));
		$query = $this->db->get();
		
		if ($query->num_rows() > 0){
			$result = $query->result_array();
			return $result;
		}else{
			return 0;
		}	
	}
}